package hu.mapro.gwtui.shared.data;

import com.google.common.base.Supplier;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.requestfactory.shared.RequestContext;

public class DataTypeUtils {

	@SuppressWarnings("unchecked")
	public static <T> Class<? extends T> getType(T object) {
		AutoBean<T> ab = AutoBeanUtils.getAutoBean(object);
		if (ab!=null) {
			return ab.getType();
		} else {
			return (Class<? extends T>) object.getClass();
		}
	}
	
	public static ClassDataFactory forRF(final RequestContext rc) {
		return new ClassDataFactory() {
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public <T> T create(Class<T> clazz) {
				Class erasure = clazz;
				return (T) rc.create(erasure);
			}
		};
	}

	public static ClassDataFactory forAB(final AutoBeanFactory rc) {
		return new ClassDataFactory() {
			@Override
			public <T> T create(Class<T> clazz) {
				return rc.create(clazz).as();
			}
		};
	}
	
	
}
